package com.xiaoxing.zhinenghuacangku_jining.mvp.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import com.innodroid.expandablerecycler.ExpandableRecyclerAdapter;
import com.jess.arms.utils.ArmsUtils;
import com.xiaoxing.zhinenghuacangku_jining.R;
import com.xiaoxing.zhinenghuacangku_jining.mvp.model.entity.JiNing_JianXiuChuKuList;

import java.util.List;
import java.util.Map;


public class JiNing_JianXiuChuKuAdapter extends ExpandableRecyclerAdapter<JiNing_JianXiuChuKuAdapter.OrderListItem> {
    public static final int TYPE_CANG_KU_NAME = 1000; //仓库名称
    public static final int TYPE_CANG_KU_PRODUCTS = 1001; //仓库商品名称
    public static final int TYPE_FOOTER = 1002; //商家订单底部统计
    String groupmid = "";
    String mid = "";
    private List<JiNing_JianXiuChuKuList> mGroups;
    private Map<String, List<String>> mChildren;
    private Map<Integer, Integer> mGroupsPosition;
    private CheckInterface checkInterface;
    private ChuKuInterface mChuKuInterface;
    private Handler handler;
    private Runnable runnable;

    public JiNing_JianXiuChuKuAdapter(Context context, List<JiNing_JianXiuChuKuList> groups, Map<String, List<String>> children, Map<Integer, Integer> groupsPosition) {
        super(context);
        this.mGroups = groups;
        this.mChildren = children;
        this.mGroupsPosition = groupsPosition;
        handler = new Handler();
    }

    public void setCheckInterface(CheckInterface checkInterface) {
        this.checkInterface = checkInterface;
    }

    public void setChuKuInterface(ChuKuInterface chuKuInterface) {
        this.mChuKuInterface = chuKuInterface;
    }

    public Object getChild(int groupPosition, int childPosition) {
        List<String> childs = mChildren.get(String.valueOf(groupPosition));
        return childs.get(childPosition);
    }

    public Object getGroup(int groupPosition) {
        return mGroups.get(groupPosition);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE_CANG_KU_NAME:
                return new ShangJiaNameViewHolder(inflate(R.layout.zhi_neng_cang_ku_ji_ning_jian_xiu_item_cang_ku_name, parent));
            case TYPE_FOOTER:
                return new FooterViewHolder(inflate(R.layout.zhi_neng_cang_ku_ji_ning_jian_xiu_item_cang_ku_footer, parent));
            case TYPE_CANG_KU_PRODUCTS:
            default:
                return new ProductsViewHolder(inflate(R.layout.zhi_neng_cang_ku_ji_ning_jian_xiu_item_cang_ku_products, parent));
        }
    }

    @Override
    public void onBindViewHolder(ExpandableRecyclerAdapter.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case TYPE_CANG_KU_NAME:
                ((ShangJiaNameViewHolder) holder).bind(position);
                break;
            case TYPE_FOOTER:
                ((FooterViewHolder) holder).bind(position);
                break;
            case TYPE_CANG_KU_PRODUCTS:
            default:
                ((ProductsViewHolder) holder).bind(position);
                break;
        }
    }

    public void setDataItems(List<OrderListItem> orderListItems) {
        setItems(orderListItems);
    }

    /**
     * 复选框接口
     */
    public interface CheckInterface {
        /**
         * 组选框状态改变触发的事件
         *
         * @param groupPosition 组元素位置
         * @param isChecked     组元素选中与否
         */
        void checkGroup(int groupPosition, boolean isChecked);

        /**
         * 子选框状态改变时触发的事件
         *
         * @param groupPosition 组元素位置
         * @param childPosition 子元素位置
         * @param isChecked     子元素选中与否
         */
        void checkChild(int groupPosition, int childPosition, boolean isChecked);
    }

    public interface ChuKuInterface {

        void chuKu(String mid, String spid, String spname);

        void queDingNaChu(String mid, String rfidTagStr);

    }

    public static class OrderListItem extends ExpandableRecyclerAdapter.ListItem {
        public JiNing_JianXiuChuKuList mOutOfTheLibraryList;
        public String mOutListBean;
        public int mGroupPosition;
        public int mChildPosition;
        public String mOutStoragelistBatchid;

        public OrderListItem(JiNing_JianXiuChuKuList outOfTheLibraryList) {
            super(TYPE_CANG_KU_NAME);
            mOutOfTheLibraryList = outOfTheLibraryList;
        }

        public OrderListItem(int groupPosition, String outListBean, int childPosition, String outStoragelistBatchid) {
            super(TYPE_CANG_KU_PRODUCTS);
            mOutListBean = outListBean;
            mGroupPosition = groupPosition;
            mChildPosition = childPosition;
            mOutStoragelistBatchid = outStoragelistBatchid;
        }

        public OrderListItem(String first, String last, String footer, String type) {
            super(TYPE_FOOTER);
        }
    }

    public class ShangJiaNameViewHolder extends ViewHolder {

        private TextView tv_name;
        private TextView tv_mid;
        private TextView tv_spname;
        private TextView tv_errormsg;
        private TextView tv_opstatus;
        private TextView tv_meterrfidtag;
        private Button tv_chu_ku;
        private Button tv_que_ding_na_chu;
        private CheckBox cb_cang_ku_name;

        public ShangJiaNameViewHolder(View view) {
            super(view);
            tv_name = view.findViewById(R.id.tv_name);
            tv_mid = view.findViewById(R.id.tv_mid);
            tv_spname = view.findViewById(R.id.tv_spname);
            tv_errormsg = view.findViewById(R.id.tv_errormsg);
            tv_opstatus = view.findViewById(R.id.tv_opstatus);
            cb_cang_ku_name = view.findViewById(R.id.cb_cang_ku_name);
            tv_meterrfidtag = view.findViewById(R.id.tv_meterrfidtag);
            tv_chu_ku = view.findViewById(R.id.tv_chu_ku);
            tv_que_ding_na_chu = view.findViewById(R.id.tv_que_ding_na_chu);
        }

        public void bind(int position) {
            try {
                JiNing_JianXiuChuKuList jianXiuChuKuList = visibleItems.get(position).mOutOfTheLibraryList;
                tv_name.setText(jianXiuChuKuList.getMaterial());
                tv_mid.setText("物料Mid：" + jianXiuChuKuList.getMid());
                tv_spname.setText("库位：" + jianXiuChuKuList.getSpname());
                tv_errormsg.setText("错误信息：" + jianXiuChuKuList.getErrormsg());
                tv_opstatus.setText("状态：" + jianXiuChuKuList.getOpstatus());
                tv_meterrfidtag.setText(("资产码：" + jianXiuChuKuList.getMeterrfidtag()).replace("[", "").replace("]", ""));

                if (jianXiuChuKuList.getOpstatus().equals("出库")) {
                    tv_chu_ku.setVisibility(View.VISIBLE);
                    tv_que_ding_na_chu.setVisibility(View.GONE);
                } else if (jianXiuChuKuList.getOpstatus().equals("确定拿出")) {
                    tv_chu_ku.setVisibility(View.GONE);
                    tv_que_ding_na_chu.setVisibility(View.VISIBLE);
                }


                final JiNing_JianXiuChuKuList group = (JiNing_JianXiuChuKuList) getGroup(mGroupsPosition.get(position));
                cb_cang_ku_name.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        checkInterface.checkGroup(mGroupsPosition.get(position), ((CheckBox) v).isChecked());// 暴露组选接口
                    }
                });

                tv_chu_ku.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        tv_chu_ku.setClickable(false);
                        tv_chu_ku.setBackground(mContext.getResources().getDrawable(R.drawable.public_shape_gray_bg_corner_5dp));
                        mChuKuInterface.chuKu(jianXiuChuKuList.getMid(), jianXiuChuKuList.getSpid(), jianXiuChuKuList.getSpname());
                    }
                });
                tv_que_ding_na_chu.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        tv_que_ding_na_chu.setClickable(false);
                        tv_que_ding_na_chu.setBackground(mContext.getResources().getDrawable(R.drawable.public_shape_gray_bg_corner_5dp));
                        String rfidTagStr = "";
                        if (jianXiuChuKuList.getMeterrfidtag() != null && jianXiuChuKuList.getMeterrfidtag().size() > 0) {

                            for (int i = 0; i < jianXiuChuKuList.getMeterrfidtag().size(); i++) {
                                rfidTagStr += jianXiuChuKuList.getMeterrfidtag().get(i) + ",";
                            }
                            rfidTagStr = ArmsUtils.cutLastString(rfidTagStr);
                            mChuKuInterface.queDingNaChu(jianXiuChuKuList.getMid(), rfidTagStr);
                        }
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }


        }
    }

    public class ProductsViewHolder extends ViewHolder {

        private TextView mTvName;

        public ProductsViewHolder(View view) {
            super(view);
            mTvName = view.findViewById(R.id.tv_name);

        }

        @SuppressLint("LongLogTag")
        public void bind(int position) {
            String outListBean = visibleItems.get(position).mOutListBean;
//            mTvName.setText("资产码:" + outListBean);
            mTvName.setText("");
            int groupPosition = visibleItems.get(position).mGroupPosition;
            int childPosition = visibleItems.get(position).mChildPosition;
            String outStoragelistBatchid = visibleItems.get(position).mOutStoragelistBatchid;

            final String goodsInfo2 = (String) getChild(groupPosition, childPosition);

        }
    }

    public class FooterViewHolder extends ViewHolder {

        private Button btn_que_ding;

        public FooterViewHolder(View view) {
            super(view);
            btn_que_ding = view.findViewById(R.id.btn_que_ding);
        }

        public void bind(int position) {
            btn_que_ding.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                }
            });
        }
    }

}
